package com.markbuikema.juliana32.model.teams;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;

import android.util.Log;

import com.markbuikema.juliana32.enums.Category;
import com.markbuikema.juliana32.enums.CompType;
import com.markbuikema.juliana32.util.Constants;
import com.markbuikema.juliana32.util.Util;

public class Team extends Observable {

	private static final String TAG = "Team model";

	private String id;
	private String name;
	private String code;
	private Category category;
	private String teamPhoto;

	private Map<CompType, League> leagues;
	private List<String> photos;

	public Team(String id, String name, Category category) {
		this.id = id;
		this.name = name;
		this.category = category;

		teamPhoto = "";
		leagues = new HashMap<CompType, League>();
		photos = new ArrayList<String>();
	}

	public boolean isCategory(Category cat) {
		return category == cat;
	}

	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public Category getCategory() {
		return category;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getCode() {
		return code;
	}

	public Map<CompType, League> getLeagues() {
		return Collections.unmodifiableMap(leagues);
	}

	public void addPhoto(String photo) {
		if (photo == null)
			return;

		// Log.d(TAG, "String added to team " + name + ", url: " + photo);
		photos.add(photo);

	}
	
	public String getTeamPhoto() {
		return teamPhoto;
	}
	
	public void setTeamPhoto(String url) {
		this.teamPhoto = url;
		Log.d("teamphoto", getName() + " model updated");
		setChanged();
		notifyObservers(Constants.CHANGED_TEAM_PHOTO);
	}

	public int getStringCount() {
		return photos.size();
	}

	public String[] getPhotoUrls() {
		String[] urls = new String[photos.size()];
		for (int i = 0; i < photos.size(); i++)
			if (photos.get(i).startsWith("http"))
				urls[i] = photos.get(i);
			else
				urls[i] = Util.PHOTO_URL_PREFIX + photos.get(i) + Util.PHOTO_URL_SUFFIX;
		return urls;
	}

	public int getPhotoCount() {
		return photos.size();
	}

	public List<String> getPhotos() {
		return Collections.unmodifiableList(photos);
	}

	public void addLeague(League league) {
		this.leagues.put(league.getType(), league);

		setChanged();
		notifyObservers(Constants.CHANGED_LEAGUES);
	}

	public void addTables(List<Table> tables) {
		for (Table t : tables) {
			leagues.get(CompType.get(t.getCompType())).addTable(t);
		}
		setChanged();
		notifyObservers(Constants.CHANGED_TABLES);
	}

	public void addPastGames(CompType type, List<GameEvent> games) {
		if (games == null || games.isEmpty() || leagues == null || leagues.get(type) == null)
			return;

		leagues.get(type).addPastGames(games);
		setChanged();
		notifyObservers(Constants.CHANGED_GAMES_PAST);
	}

	public void addUpcomingGames(CompType type, List<GameEvent> games) {
		if (games == null || games.isEmpty() || leagues == null || leagues.get(type) == null)
			return;

		leagues.get(type).addUpcomingGames(games);
		setChanged();
		notifyObservers(Constants.CHANGED_GAMES_UPCOMING);
	}

}
